//
//  DSViewController.m
//  DS爱信
//
//  Created by 秦建英 on 14-5-27.
//  Copyright (c) 2014年 秦建英. All rights reserved.
//

#import "DSViewController.h"
#import "DSMianViewController.h"
#import "DSDialViewController.h"
@interface DSViewController ()

@end

@implementation DSViewController
{
    BOOL hidden;
    DSDialViewController *_dial;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _dial =[[DSDialViewController alloc]init];
        }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self createViewControllers];
}
-(void)createViewControllers
{
    hidden=NO;
    //取plist文件
    NSString *path =[[NSBundle mainBundle]pathForResource:@"Data" ofType:@"plist"];
    NSArray *array =[NSArray arrayWithContentsOfFile:path];
        //收集视图控制器对象
    NSMutableArray *controllers =[NSMutableArray array];
    for(NSDictionary *dic in array)
    {
        //取到类名
        NSString *className =[dic objectForKey:@"classname"];
        //NSClassFromString 根据类名获取到对应的类
        //class 为类的泛型    id为对象的泛型
        Class class =NSClassFromString(className);
        //用不同的类进行alloc init.得到的对象，可以用他们共同的父类指针来接收，得益于oc 的动态运行时机制，会把root 按照不同的视图控制器来处理
        DSMianViewController *root =[[class alloc]init];
        //设置标题
        root .title =[dic objectForKey:@"title"];
        //
        NSString *imageName =[dic objectForKey:@"imageName"];
        //
        NSString *selectImageName =[imageName stringByAppendingFormat:@"_pressed"];
        //使用高版本才有的方法之前，先判断
        if([root.tabBarItem respondsToSelector:@selector(initWithTitle:image:selectedImage:)])
        {
            //ios 7
            UITabBarItem *item =[[UITabBarItem alloc]initWithTitle:[dic objectForKey:@"title"] image:[UIImage imageNamed:imageName] selectedImage:[UIImage imageNamed:selectImageName]];
            root.tabBarItem =item;
        }else
        {//ios 6
            [root.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:selectImageName] withFinishedUnselectedImage:[UIImage imageNamed:imageName]];
        }
        [controllers addObject:root];
    }
    self.viewControllers =controllers;
}
//调整子视图
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if([tabBar.selectedItem.title isEqualToString:@"拨号"])
    {
        if(hidden==NO)
        {
            [UIView animateWithDuration:0.01 animations:^{
                [[NSNotificationCenter defaultCenter]postNotificationName:@"hidden" object:_dial.views];
            } completion:^(BOOL finished) {
                hidden=YES;
            }];
        }
        if(hidden==YES)
        {
            [UIView animateWithDuration:0.01 animations:^{
                [[NSNotificationCenter defaultCenter]postNotificationName:@"hid" object:_dial.views];
            } completion:^(BOOL finished) {
                hidden=NO;
            }];
        }
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
